<h2>Заказ номер <?= $order->id ?></h2>
<br/>
<div class="scrollable">
	<div class="tabulation" data-id="tabs-1">
		<a class="element active" data-id="tab-1">Товары</a>
		<a class="element " data-id="tab-2">Информация</a>
	</div>
	<form method="POST" action="<?= CHtml::normalizeUrl(array('orders/SaveOrder', 'id'=>$order->id))?>">
	<div class="tabs" id="tabs-1">
		<div class="tab active" id="tab-1">

			<table class="table">
				<thead>
					<tr>
						<td>Изображение</td>
						<td>Код</td>
						<td>Наименование</td>
						<td>Кол-во</td>
						<td>Цена за ед.</td>
						<td>Сумма</td>
						<td>Действие</td>
					</tr>
				</thead>
				<tbody>
						<? foreach ($order->ordersItems as $item): ?>
						<tr class="items_all">
							<td><div class="icon50 zoom"><img src="<?=$item->idItem->img()?>" class="round50"></div></td>
							<td id="id_item"> <?= $item->idItem->id ?> </td>
							<td> <?= $item->idItem->title ?> </td>
							<td> <input class="count" type="text" name="items[<?=$item->idItem->id?>]" value="<?= $item->count ?>"></td>
							<td> <span id="price"><?= $item->price ?></span> тенге</td>
							<td><span id="fprice"><?= $item->price*$item->count ?></span> тенге</td>
							<td class="skip">
								<div class="btn-group">
									<a class="btn btn-green icon delete-item" data-id="<?=$item->id?>" href="#">g</a>
								</div>
							</td>
						</tr>
						<? endforeach; ?>
						<tr id="new_item">
							<td>
								<select class='items' id="new_item">
									<? foreach ($items_new as $item_new): ?>
										<option value="<?= $item_new->id ?>" ><?= $item_new->title ?></option>
									<? endforeach ?>
								</select>
							</td>
							<td class="skip">
								<div class="btn-group">
									<a class="btn btn-green add-item" href="#">+ Добавить</a>
								</div>
							</td>
						</tr>
						<tr>
							<td colspan="8" class="no_border">
								<div style=" float:right;"><div style="float: left;padding-right: 10px; text-align: right;">Сумма заказа:</div><div style="float: left;"><span id="order_summ"><?=$order->full_price?></span> тенге.</div></div>
							</td>
						</tr>
<!--						<tr>-->
<!--							<td colspan="8" class="no_border">-->
<!--								<div style="float:right;"><div style="float: left;padding-right: 10px; text-align: right;">Стоимость доставки:</div><div style="float: left;"><span><input type="text" name="order[delivery_price]" id="delivery_price" value="--><?//= $order->delivery_price ?><!--"><span> тенге.</div></div>-->
<!--							</td>-->
<!--						</tr>-->
						<tr>
							<td colspan="8" class="no_border">
								<div style="float:right;"><div style="float: left;padding-right: 10px; text-align: right;">К оплате:</div><div style="float: left; "><span id="full_price" ><?=$order->full_price?></span> тенге.</div></div>
							</td>
						</tr>
				</tbody>
			</table>
		</form>
		</div>
		<div class="tab" id="tab-2">
			<table>
				<tbody>
					<tr>
						<td><strong>Номер заказа</strong></td>
						<td><?= $order->id ?></td>
					</tr>
					<tr>
						<td><strong> Имя клиента </strong></td>
						<td>
							<input type="text" class="input" value="<?= $order->name ?>" name="order[name]">
						</td>
					</tr>
					<tr>
						<td><strong>Телефон клиента</strong></td>
						<td>
							<input type="text" class="input" value="<?= $order->phone ?>" name="order[phone]">
						</td>
					</tr>
					<tr>
						<td><strong>Электронный адрес клиента </strong></td>
						<td>
							<input type="text" class="input" value="<?=  $order->email; ?>" name="order[email]">
						</td>
					</tr>
					<tr>
						<td><strong>Дата и время поступления заказа</strong></td>
						<td><input type="text" class="input" value="<?= $order->date_order ?>" name="order[date_order]"></td>
					</tr>
<!--					<tr>-->
<!--						<td><strong>Дата желаемой доставки</strong></td>-->
<!--						<td><input type="text" class="input" value="--><?//= $order->date_delivery ?><!--" name="order[date_delivery]"></td>-->
<!--					</tr>-->
					<tr>
						<td><strong>Адрес доставки</strong></td>
						<td><input type="text" class="input" value="<?= $order->address ?>" name="order[address]"></td>
					</tr>
					<tr>
						<td><strong>Пожелания</strong></td>
						<td colspan="3">
							<textarea name="order[message]"  class="input wide"><?=$order->message?></textarea>
						</td>
					</tr>
					<tr>
						<td></td>
						<td></td>
					
					</tr>
				</tbody>
			</table>
		</div>
	<input type="submit" class="btn-green save-btn" value="Сохранить">
	</div>
</form>
</div>

<script type="text/javascript">
	function update_price() {
		var summ = 0;
//		var delivery =parseInt($('#delivery_price').val());
		var delivery =parseInt(0);
		$('.items_all').each(function(){
			summ = summ+parseInt($(this).find('#fprice').html());
		});
		$('#order_summ').html(summ);

		$('#full_price').html(summ+ delivery)
	}
	$(function(){

		$('.items').chosen();

		$('.popup').on('change', '.count', function() {
			var count=this.value;
			var price= parseInt($(this).parents('tr').find('#price').html());
			$(this).parents('tr').find('#fprice').html(price*count);
			update_price();

		}).on('click', '.delete-item', function(e) {
			e.preventDefault();
			var isConfirmed = confirm('Вы уверены?');
			if (isConfirmed) {
				var id=$(this).data('id');
				$(this).parents('tr').remove();
				update_price();
			}
		});
		$('#delivery_price').change(function(){
			update_price();
		});
		$('.add-item').click(function(e){
			e.preventDefault();
			var id=$('.items').val();
			var add=true;
			$('.items_all').each(function(){
				var id_old= parseInt($(this).find('#id_item').html());
				if(id==id_old){
					 add=false;
				}
			});
			if (add==true) {
				$.ajax({
					url: '<?= CHtml::normalizeUrl(array('orders/AddItem'))?>',
					type: 'GET',
					data: {id: id},
					success: function (data) {
						$('#new_item').before(data);
						update_price();
						humane.log('Товар успешно добавлен');
					}
				})
			}else{
				humane.log('Данный товар уже есть в заказе');
			}
		});

		$('.tabulation .element').click(function() {
			$('.tabulation .element').removeClass('active');
			$(this).addClass('active');
			$('#' + $(this).parent().data('id') + ' > .tab').hide();

			$('#' + $(this).data('id')).show();
		});
		
	});
</script>